added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2010 / VBWPFMVVMPractice / AttachedBehavior / ShutdownBehavior.vb
blobc5fc7af27e2a667e8df86a4f43d3a1a88c2d9f12
1 '****************************** Module Header ******************************\
2 ' Module Name: ShutdownBehavior.vb
3 ' Project: VBWPFMVVMPractice
4 ' Copyright (c) Microsoft Corporation.
5 '
6 ' The ShutdownBehavior class is an attached behavior that is used to shut down the application.
7 '
8 ' This source is subject to the Microsoft Public License.
9 ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
10 ' All other rights reserved.
12 ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
13 ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
14 ' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
15 '***************************************************************************/
17 Public Class ShutdownBehavior
18 Inherits DependencyObject
20 Public Shared Function GetForceShutdown(ByVal obj As DependencyObject) As Nullable(Of Boolean)
21 Return DirectCast(obj.GetValue(ShutdownBehavior.ForceShutdownProperty), Nullable(Of Boolean))
22 End Function
24 Public Shared Sub SetForceShutdown(ByVal obj As DependencyObject, ByVal value As Nullable(Of Boolean))
25 obj.SetValue(ShutdownBehavior.ForceShutdownProperty, value)
26 End Sub
28 ' Using a DependencyProperty as the backing store for ForceShutdown. This enables
29 ' animation, styling, binding, etc...
30 Public Shared ReadOnly ForceShutdownProperty As DependencyProperty =
31 DependencyProperty.RegisterAttached(
32 "ForceShutdown",
33 GetType(Nullable(Of Boolean)),
34 GetType(ShutdownBehavior),
35 New UIPropertyMetadata(
36 Nothing,
37 Function(sender As DependencyObject, e As DependencyPropertyChangedEventArgs)
38 ShutdownBehavior.OnPropertyChangedCallBack(sender, e)
39 End Function))
41 ''' <summary>
42 ''' MenuItem's Click event handler
43 ''' </summary>
44 Private Shared Sub mnu_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
46 ' If the ForceShutdown attached proerty value set on the MenuItem is true, exit the
47 ' application immediately.
48 If ShutdownBehavior.GetForceShutdown(TryCast(sender, DependencyObject)).Value Then
49 Application.Current.Shutdown()
51 ' If the ForceShutdown attached proerty value set on the MenuItem is false, show a
52 ' messagebox before exiting the application.
53 ElseIf (Not ShutdownBehavior.GetForceShutdown(TryCast(sender, DependencyObject)).Value AndAlso
54 (MessageBox.Show("Are you sure to exit the application?",
55 "Exit", MessageBoxButton.OKCancel) = MessageBoxResult.OK)) Then
56 Application.Current.Shutdown()
57 End If
58 End Sub
60 ''' <summary>
61 ''' The ForceShutdown property's PropertyChangedCallback method.
62 ''' In this method, get the MenuItem the attached property is set on and subscribe to the
63 ''' Click event of the MenuItem.
64 ''' </summary>
65 Private Shared Sub OnPropertyChangedCallBack(ByVal sender As DependencyObject,
66 ByVal e As DependencyPropertyChangedEventArgs)
67 Dim mnu As MenuItem = TryCast(sender, MenuItem)
69 ' This ensures the Click event on the MenuItem is subscribed to only once.
70 If (Not mnu Is Nothing) Then
71 RemoveHandler mnu.Click, New RoutedEventHandler(AddressOf ShutdownBehavior.mnu_Click)
72 AddHandler mnu.Click, New RoutedEventHandler(AddressOf ShutdownBehavior.mnu_Click)
73 End If
74 End Sub
76 End Class